package net.halflite.mzkr.entity;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import net.halflite.mzkr.type.UserStatusType;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * RegistedUserエンティティクラス
 * 
 */
@Entity
public class RegistedUser implements Serializable {

    private static final long serialVersionUID = 1L;

    /** ユーザー名最小長 */
    public static final int MIN_USERNAME_LENGTH = 4;

    /** ユーザー名最大長 */
    public static final int MAX_USERNAME_LENGTH = 64;

    /** パスワード最小長 */
    public static final int MIN_PASSWORD_LENGTH = 4;

    /** パスワード最大長 */
    public static final int MAX_PASSWORD_LENGTH = 64;

    /** メールアドレス最大長 */
    public static final int MAX_ADDRESS_LENGTH = 255;

    /** ログイン不可能状態 */
    private static final List<UserStatusType> DENIED_LOGIN =
    	Arrays.asList(new UserStatusType[]{UserStatusType.REGISTED, UserStatusType.QUIT});
    
    /** idプロパティ */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long id;

    /** usernameプロパティ */
    public String username;

    /** addressプロパティ */
    public String address;

    /** passwordプロパティ */
    public String password;

    /** statusプロパティ */
    @Enumerated(EnumType.STRING)
    public UserStatusType status;

    /** createdプロパティ */
    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    public Date created;

    /** modifiedプロパティ */
    @Temporal(TemporalType.TIMESTAMP)
    public Date modified;

    /** tempUserList関連プロパティ */
    @OneToMany(mappedBy = "registedUser")
    public List<TempUser> tempUsers;

    /** userProfileList関連プロパティ */
    @OneToMany(mappedBy = "registedUser")
    public List<UserProfile> userProfiles;
    
    /** userProfileList関連プロパティ */
    @OneToMany(mappedBy = "registedUser")
    public List<UserImage> userImages;

	public RegistedUser(String username, String address) {
		this.username = username;
		this.address = address;
		created = new Date();
		modified = new Date();
	}

	public RegistedUser() {
	}

	/** ログイン可能状態判定 */
	public boolean isLoginable() {
		return !DENIED_LOGIN.contains(status);
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
	
	
}